﻿using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Button button1;
        public MainWindow()
        {
            //InitializeComponent();

            //MyInit();

            InitMainWindw();
        }


        private void InitMainWindw()
        {
            string fileXaml = string.Empty;
            MyInit2(fileXaml);

        }

        private void MyInit2(string fileXaml)
        {
            //1.窗口相关属性
            this.Width = this.Height = 285;
            this.Left = this.Top = 100;
            this.Title = "Code-Only Window";

            //2.可以从xaml文件种读取
            using (FileStream fileStream = new FileStream(fileXaml, FileMode.Open))
            {
                DependencyObject dependencyObject = (DependencyObject)XamlReader.Load(fileStream);

                //3.从xaml种获得节点
                button1 = (Button)LogicalTreeHelper.FindLogicalNode(dependencyObject, "button1");
                button1.Click += button1_Click;

                //this.Content = button1;
                this.AddChild(button1);
            }

        }

        private void MyInit()
        {
            //1.窗口相关属性
            this.Width = this.Height = 285;
            this.Left = this.Top = 100;
            this.Title = "Code-Only Window";

            //2.创建button
            button1 = new Button();
            button1.Content = "Please click me.";
            button1.Margin = new Thickness(10);
            button1.Click += button1_Click;

            //3.需要将button放入容器
            DockPanel dockPanel = new DockPanel();

            IAddChild container = dockPanel;
            container.AddChild(button1);

            //4.需要将当前内容加到窗口种
            this.AddChild(container);
        }



        private void button1_Click(object sender, RoutedEventArgs e)
        {
            button1.Content = "Thank you.";
        }
    }
}